package cn.tedu.online.day04;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class ConsumerBlocking {
    public static void main(String[] args) {
        BlockingQueue<String> queue = new LinkedBlockingQueue<>();

        Thread producer = new Thread(){
            @Override
            public void run() {
                for (int i=0;i<100;i++) {
                    try {
                        Thread.sleep(2000);
                        queue.put("hello"+i);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        Thread consumer = new Thread(){
            @Override
            public void run() {
                while (true){
                    try {
                        String ele = queue.take();
                        System.out.println("取出"+ele);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };

        producer.start();
        consumer.start();
    }
}
